﻿<%@ Control Language="C#" %>
<%@ Register TagPrefix="Hi" Namespace="Hidistro.UI.Common.Controls" Assembly="Hidistro.UI.Common.Controls" %>
<%@ Register TagPrefix="Hi" Namespace="Hidistro.UI.SaleSystem.Tags" Assembly="Hidistro.UI.SaleSystem.Tags" %>
<%@ Import Namespace="Hidistro.Core" %>

<asp:Panel ID="pnlShopProductCart" runat="server">
    <asp:DataList ID="dataListShoppingCrat" runat="server" Width="100%">
        <HeaderTemplate>
            <table width="100%" cellspacing="0" cellpadding="0" border="0" class="cart_commodit_th">
                <tbody>
                    <tr>
                        <th width="30">
                            <input type="checkbox" checked="checked" id="wnxd_qx" /></th>
                        <th width="80">商品图片</th>
                        <th width="350">商品名称</th>
                        <th width="150">商品单价</th>
                        <th width="120">购买数量</th>
                        <th width="150">小计</th>
                        <th width="150">操作</th>
                    </tr>
                    <Hi:wnxd_PromotionsColumn runat="server" />
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td colspan="7">
                    <table width="100%" cellspacing="0" cellpadding="0" border="0" class="cart_commodit_con">
                        <tbody>
                            <tr>
                                <td width="30" align="center">
                                    <input type="checkbox" id="ck_<%# Eval("SkuId") %>" name="ck_productId" value="<%# Eval("SkuId") %>" checked="checked" /></td>
                                <td width="80" align="center">
                                    <span class="cart_commodit_span">
                                        <Hi:ProductDetailsLink ID="ProductDetailsLink2" ProductId='<%# Eval("ProductId")%>' ProductName='<%# Eval("Name")%>' runat="server" ImageLink="true">
<Hi:ListImage ID="ListImage1" DataField="ThumbnailUrl60" runat="server" />
                                        </Hi:ProductDetailsLink>
                                    </span>
                                </td>
                                <td width="350" align="left" style="text-align: left; padding-left: 25px;">
                                    <div class="cart_commodit_name">
                                        <Hi:ProductDetailsLink ID="ProductDetailsLink1" ProductId='<%# Eval("ProductId")%>' ProductName='<%# Eval("Name")%>' runat="server" />
                                    </div>
                                    <div class="cart_commodit_para">
                                        <asp:Literal ID="litSKUContent" runat="server" Text='<%# Eval("SKUContent") %>'></asp:Literal>
                                    </div>
                                </td>
                                <td width="150" align="center"><span class="cart_commodit_price">￥<Hi:FormatedMoneyLabel ID="FormatedMoneyLabel1" runat="server" Money='<%# Eval("MemberPrice") %>' /></span></td>
                                <td width="120" align="center">
                                    <div class="yb">
                                        <asp:Literal runat="server" ID="litSkuId" Text='<%# Eval("SkuId")%>' Visible="false"></asp:Literal>
                                    </div>
                                    <asp:Button ID="Button3" runat="server" CssClass="cart_update wnxd_sub" Text="-" CommandName="updateBuyNum" SubmitTagID='<%# Eval("SKU")%>' />
                                    <asp:TextBox runat="server" ID="txtBuyNum" Text='<%# Eval("Quantity")%>' Width="30" CssClass="cart_txtbuynum" inputTagID='<%# Eval("SKU")%>' />
                                    <asp:Button ID="Button1" runat="server" CssClass="cart_update wnxd_add" Text="+" CommandName="updateBuyNum" SubmitTagID='<%# Eval("SKU")%>' />
                                    <div style="margin-top: 5px;">
                                        <asp:Literal ID="litGiveQuantity" Text='<%# (int)Eval("Quantity")==(int)Eval("ShippQuantity")?"":"赠送："+((int)Eval("ShippQuantity")-(int)Eval("Quantity")) %>' runat="server" />
                                    </div>
                                    <asp:HyperLink ID="hlinkPurchase" runat="server" NavigateUrl='<%# string.Format(Globals.GetSiteUrls().UrlData.FormatUrl("FavourableDetails"),  Eval("PromotionId"))%>'
                                        Text='<%# Eval("PromotionName")%>' Target="_blank"></asp:HyperLink>
                                </td>
                                <tsd width="150" align="center"><%--<span class="cart_commodit_price2">￥<Hi:FormatedMoneyLabel ID="FormatedMoneyLabel2" runat="server" Money='<%# Eval("SubTotal") %>' /></span>--%></tsd>
                                <td width="150" align="center">
                                    <asp:Button ID="Button2" runat="server" CssClass="cart_commodit_del" CommandName="delete"
                                        Text="删除" />
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </td>
            </tr>

        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:DataList>
    <script type="text/javascript" src="utility/jquery.artdialog.js"></script>
    <link type="text/css" rel="stylesheet" href="utility/skins/opera.css" />
    <script type="text/javascript">
        $(".wnxd_sub").click(function () {
            var $parent = $(this).parent(),
                $this = $parent.children(".cart_txtbuynum"),
                $sku = $parent.parent().find("[name='ck_productId']").val();
            $v = parseInt($this.val());
            if ($v > 1) {
                $v--;
                var list = [],
                    $select = $("[name='ck_productId']");
                for (var i = 0; i < $select.length; i++) {
                    var $t = $select[i];
                    if (!$t.checked) list.push($t.value);
                }
                $.post(location.href, "wnxd_Ajax=UpdateNum&num=" + $v + "&SkuId=" + $sku + "&list=" + JSON.stringify(list), function (e) {
                    if (e.error) {

                    }
                    else {
                        $this.val($v);
                        $("#ShoppingCart_lblTotalPrice").text(e.price.toFixed(2));
                    }
                }, "json");
            }
            return false
        });
        $(".wnxd_add").click(function () {
            var $parent = $(this).parent(),
                $this = $parent.children(".cart_txtbuynum"),
                $sku = $parent.parent().find("[name='ck_productId']").val(),
                $v = parseInt($this.val());
            $v++;
            var list = [],
                $select = $("[name='ck_productId']");
            for (var i = 0; i < $select.length; i++) {
                var $t = $select[i];
                if (!$t.checked) list.push($t.value);
            }
            $.post(location.href, "wnxd_Ajax=UpdateNum&num=" + $v + "&SkuId=" + $sku + "&list=" + JSON.stringify(list), function (e) {
                if (e.error) {

                }
                else {
                    $this.val($v);
                    $("#ShoppingCart_lblTotalPrice").text(e.price.toFixed(2));
                }
            }, "json");
            return false;
        });
        $(document).ready(function () {
            $("input[inputTagID]").each(function (i, obj) {
                var $this = $(this);
                $this.keydown(function (obj) {
                    var key = window.event ? obj.keyCode : obj.which;
                    if (key == 13) {
                        $this.blur();
                        return false;
                    }
                }).blur(function () {
                    var $this = $(this);
                    if ($this.val() == "") {
                        $this.focus();
                        return false;
                    }
                    var $parent = $this.parent(),
                        $sku = $parent.parent().find("[name='ck_productId']").val(),
                        $v = parseInt($this.val()),
                        list = [],
                        $select = $("[name='ck_productId']");
                    for (var i = 0; i < $select.length; i++) {
                        var $t = $select[i];
                        if (!$t.checked) list.push($t.value);
                    }
                    $.post(location.href, "wnxd_Ajax=UpdateNum&num=" + $v + "&SkuId=" + $sku + "&list=" + JSON.stringify(list), function (e) {
                        if (e.error) {

                        }
                        else {
                            $this.val($v);
                            $("#ShoppingCart_lblTotalPrice").text(e.price.toFixed(2));
                        }
                    }, "json");
                }).keyup(function () {
                    if (this.value.length == 1) {
                        this.value = this.value.replace(/[^1-9]/g, '');
                    } else {
                        this.value = this.value.replace(/\D/g, '');
                    }
                }).bind("onbeforepaste", function () {
                    var str = clipboardData.getData('text');
                    if (str.length == 1) {
                        str = str.replace(/[^1-9]/g, '');
                    }
                    else {
                        str = str.replace(/\D/g, '');
                    }
                    clipboardData.setData('text', str);
                });
            })
        });
        (function () {
            var $select = $("[name='ck_productId']"),
                NotSelect = <%=((Common_ShoppingCart_ProductList)this.Parent).NotSelect%>;
            if (NotSelect != undefined && NotSelect.length > 0){
                $("#wnxd_qx").attr("checked", false);
                for(var i=0;i<NotSelect.length;i++){
                    $("[name='ck_productId'][value='"+NotSelect[i]+"']").attr("checked",false);
                }
            }
            $(theForm).bind("submit", function(){
                var list = [];
                for (var i = 0; i < $select.length; i++) {
                    var $this = $select[i];
                    if (!$this.checked) list.push($this.value);
                }
                if (list.length == $select.length){
                    art.dialog({
                        title: "错误",
                        content: "请选择要购买的商品",
                        cancel: true,
                        fixed: true
                    });
                    return false;
                }
                var $NotBuy = $("#NotBuy");
                if ($NotBuy.length == 0){
                    $NotBuy = $('<input type="hidden" name="NotSelect" id="NotBuy" />').appendTo(theForm);
                }
                $NotBuy.val(JSON.stringify(list));
            });
            $("#wnxd_qx").bind("click", function () {
                $select.attr("checked", this.checked);
                var list = [];
                for (var i = 0; i < $select.length; i++) {
                    var $this = $select[i];
                    if (!$this.checked) list.push($this.value);
                }
                $.post(location.href, "wnxd_Ajax=UpdateList&list=" + JSON.stringify(list), function (e) {
                    if (e.error) {

                    }
                    else {
                        $("#ShoppingCart_lblTotalPrice").text(e.price.toFixed(2));
                    }
                }, "json");
            });
            $select.click(function () {
                var list = [];
                for (var i = 0; i < $select.length; i++) {
                    var $this = $select[i];
                    if (!$this.checked) list.push($this.value);
                }
                if ($("#ShoppingCart_hfdIsLogin").val() == "logined") {
                    var $form = $('<form method="post" action="ShoppingCart.aspx"></form>').hide();
                    $('<input type="hidden" name="list" />').val(JSON.stringify(list)).appendTo($form);
                    $form.appendTo("body");
                    $form.submit();
                    return;
                }
                if (!$select.is("[checked]")) {
                    $("#wnxd_qx").attr("checked", false);
                }
                else if ($select.not("[checked]").length == 0) {
                    $("#wnxd_qx").attr("checked", true);
                }
                $.post(location.href, "wnxd_Ajax=UpdateList&list=" + JSON.stringify(list), function (e) {
                    if (e.error) {

                    }
                    else {
                        $("#ShoppingCart_lblTotalPrice").text(e.price.toFixed(2));
                    }
                }, "json");
            });
        })();
    </script>
</asp:Panel>
